package com.neteast.androidclient.contact.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.Log;
import com.neteast.androidclient.contact.Constant;
import com.neteast.androidclient.contact.activity.MainActivity;
import com.neteast.androidclient.contact.domain.Contact;
import com.neteast.androidclient.contact.domain.ContactData;

public class NetUtil {
	private NetUtil() {}
	/**云好友分组id和分组名映射关系表*/
	public static final HashMap<String, Integer> cloudgroupMap=new HashMap<String, Integer>();
	/**通讯录分组id和分组名映射关系表*/
	public static final HashMap<String, Integer> contactgroupMap=new HashMap<String, Integer>();
	/**云好友*/
	public static final int CLOUD = 1;
	/**通讯录好友*/
	public static final int CONTACTS = 0;
	
	/**
	 * 得到联系人所有的云好友
	 * @param context
	 * @param userId
	 * @return 
	 * @throws Exception
	 */
	public static ArrayList<Contact> getCloudFriends(Context context,int userId) throws Exception {
		String path=LoginUtil.getBaseUrl(context)+"/Usercenter/yuserlist";
		ArrayList<Contact> list = getContactListFromServer(context,path);
		if (list!=null) {
			MainActivity.cloudFriends.clear();
			MainActivity.cloudFriends.addAll(list);
			context.sendBroadcast(new Intent(Constant.ACTION_DOWNLOAD_HAS_READY));
			return list;
		}else {
			throw new RuntimeException("云好友加载失败");
		}
		
	}
	/**
	 * 得到联系人所有的通讯录好友
	 * @param context
	 * @param userId
	 * @return 
	 * @throws Exception
	 */
	public static ArrayList<Contact> getContactFriends(Context context,int userId) throws Exception {
		String path=LoginUtil.getBaseUrl(context)+"/Usercenter/userlist";
		ArrayList<Contact> list = getContactListFromServer(context,path);
		if (list!=null) {
			MainActivity.contactFriends.clear();
			MainActivity.contactFriends.addAll(list);
			context.sendBroadcast(new Intent(Constant.ACTION_DOWNLOAD_HAS_READY));
			return list;
		}else {
			throw new RuntimeException("数据加载失败");
		}
		
	}
	
	private static ArrayList<Contact> getContactListFromServer(Context context, String path)throws Exception {
		
		DefaultHttpClient client=new DefaultHttpClient();
		int userId=LoginUtil.getLastLoginUserId(context);
		String token=LoginUtil.getLastLoginToken(context);
		HttpPost post=new HttpPost(path);
		ArrayList<NameValuePair> valuePairs=new ArrayList<NameValuePair>();
		
		BasicNameValuePair valuePair=new BasicNameValuePair("appcode",LoginUtil.APPCODE);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("datatype","j");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("token",token);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("userid",userId+"");
		valuePairs.add(valuePair);
		
		UrlEncodedFormEntity entity=new UrlEncodedFormEntity(valuePairs, "UTF-8");
		post.setEntity(entity);
		HttpResponse response = client.execute(post);
		HttpEntity httpEntity = response.getEntity();
		if (httpEntity!=null) {
			return JsonUtil.parseContacts(context, httpEntity.getContent());
		}else {
			throw new RuntimeException("获取用户列表失败");
		}
	}
	/**
	 * 获取联系人的详细信息
	 * @param context
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public static Contact getContactFromServer(Context context,int userId) throws Exception {
		String path=LoginUtil.getBaseUrl(context)+"/Usercenter/userdetail?contactid="+userId+"&&datatype=j";
		URL url=new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(10 * 1000);
		conn.setRequestMethod("GET");
		if (conn.getResponseCode() == 200) {
			return JsonUtil.parseContact(context,conn.getInputStream());
		} else {
			throw new RuntimeException("数据加载失败");
		}
	}
	/**
	 * 根据首字母获取分组内的云好友
	 * @param c
	 * @return
	 */
	public static ArrayList<Contact> getCloudFilterByLetter(char c){
		return filterByLetter(c, MainActivity.cloudFriends);
	}
	/**
	 * 根据首字母获取分组内的通讯录好友
	 * @param c
	 * @return
	 */
	public static ArrayList<Contact> getContactFilterByLetter(char c){
		return filterByLetter(c, MainActivity.contactFriends);
	}
	
	private static ArrayList<Contact> filterByLetter(char c,ArrayList<Contact> baseList) {
		ArrayList<Contact> list=new ArrayList<Contact>();
		int size = baseList.size();	
		for (int i = 0; i < size; i++) {
			Contact contact = baseList.get(i);
			if (contact.sortKey==c) {
				list.add(contact);
			}
		}
		return list;
	}
	/**
	 * 编辑联系人
	 * @param context
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static Contact editContact(Context context,JSONObject obj) throws Exception{
		String path=LoginUtil.getBaseUrl(context)+"/Usercenter/useredit";
		Contact contact = upLoadToServer(context, path, obj);
		if (contact==null) {
			throw new RuntimeException("编辑联系人失败");
		}
		return contact;
	}
	/**
	 * 新增联系人
	 * @param context
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static Contact addContact(Context context,JSONObject obj) throws Exception{
		String path=LoginUtil.getBaseUrl(context)+"/Usercenter/useradd";
		Contact contact = upLoadToServer(context, path, obj);
		if (contact==null) {
			throw new RuntimeException("新增联系人失败");
		}
		return contact;
	}
	/**
	 * 删除联系人
	 * @param userId
	 * @throws Exception
	 */
	public static void deleteContact(Context context,Contact contact) throws Exception {
		String path=LoginUtil.getBaseUrl(context)+"/Usercenter/userdel";
		String photo = ImageUtil.getPhoto(context, contact.contactId);
		if (photo!=null) {
			new File(photo).delete();
		}
		DefaultHttpClient client=new DefaultHttpClient();
		int userId=LoginUtil.getLastLoginUserId(context);
		String token=LoginUtil.getLastLoginToken(context);
		String verify=MD5Util.phpMD5Encode(userId+""+LoginUtil.KEY);
		HttpPost post=new HttpPost(path);
		ArrayList<NameValuePair> valuePairs=new ArrayList<NameValuePair>();
		
		BasicNameValuePair valuePair=new BasicNameValuePair("appcode",LoginUtil.APPCODE);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("verify",verify);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("datatype","j");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("token",token);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("userid",userId+"");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("deleteid",contact.contactId+"");
		valuePairs.add(valuePair);
		
		UrlEncodedFormEntity entity=new UrlEncodedFormEntity(valuePairs, "UTF-8");
		post.setEntity(entity);
		HttpResponse response = client.execute(post);
		HttpEntity httpEntity = response.getEntity();
		if (httpEntity!=null) {
			JsonUtil.parseBasePostResponse(context, httpEntity.getContent());
		}
	}
	private static Contact upLoadToServer(Context context,String path,JSONObject obj) throws Exception{
		
		DefaultHttpClient client=new DefaultHttpClient();
		int userId=LoginUtil.getLastLoginUserId(context);
		String token=LoginUtil.getLastLoginToken(context);
		String verify=MD5Util.phpMD5Encode(userId+""+obj.toString()+LoginUtil.KEY);
		
		HttpPost post=new HttpPost(path);
		ArrayList<NameValuePair> valuePairs=new ArrayList<NameValuePair>();
		
		BasicNameValuePair valuePair=new BasicNameValuePair("appcode",LoginUtil.APPCODE);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("verify",verify);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("datatype","j");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("token",token);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("userid",userId+"");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("json_string",obj.toString());
		valuePairs.add(valuePair);
		
		UrlEncodedFormEntity entity=new UrlEncodedFormEntity(valuePairs, "UTF-8");
		post.setEntity(entity);
		HttpResponse response = client.execute(post);
		HttpEntity httpEntity = response.getEntity();
		if (httpEntity!=null) {
			return JsonUtil.parseContact(context, httpEntity.getContent());
		}
		return null;
	}
	/**
	 * 得到云好友分组
	 * @param context
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public static ArrayList<String> getCloudGroups(Context context,int userId) throws Exception{
		String path=LoginUtil.getBaseUrl(context)+"/Usercenter/ygroup";
		ArrayList<String> groups = getGroups(context, path, cloudgroupMap);
		if (groups!=null) {
			MainActivity.cloudGroups.clear();
			MainActivity.cloudGroups.addAll(groups);
			return groups;
		}
		throw new RuntimeException("获取分组失败");
	}
	/**
	 * 得到通讯录分组
	 * @param context
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public static ArrayList<String> getContactGroups(Context context,int userId) throws Exception{
		String path=LoginUtil.getBaseUrl(context)+"/Usercenter/group";
		ArrayList<String> groups = getGroups(context, path, contactgroupMap);
		if (groups!=null) {
			MainActivity.contactGroups.clear();
			MainActivity.contactGroups.addAll(groups);
			return groups;
		}
		throw new RuntimeException("获取分组失败");
	}
	
	private static ArrayList<String> getGroups(Context context,String path,HashMap<String, Integer> map) throws Exception{
		DefaultHttpClient client=new DefaultHttpClient();
		int userId=LoginUtil.getLastLoginUserId(context);
		String token=LoginUtil.getLastLoginToken(context);
		
		HttpPost post=new HttpPost(path);
		ArrayList<NameValuePair> valuePairs=new ArrayList<NameValuePair>();
		
		BasicNameValuePair valuePair=new BasicNameValuePair("appcode",LoginUtil.APPCODE);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("datatype","j");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("token",token);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("userid",userId+"");
		valuePairs.add(valuePair);
		
		UrlEncodedFormEntity entity=new UrlEncodedFormEntity(valuePairs, "UTF-8");
		post.setEntity(entity);
		HttpResponse response = client.execute(post);
		HttpEntity httpEntity = response.getEntity();
		if (httpEntity!=null) {
			return JsonUtil.parseGroups(context,httpEntity.getContent(),map);
		}
		return null;
	}
	/**
	 * 增加云好友分组
	 * @param userId
	 * @param groupName
	 * @throws Exception
	 */
	public static void addCloudGroup(Context context,String groupName) throws Exception {
		String g = URLEncoder.encode(groupName, "UTF-8");
		String path=LoginUtil.getBaseUrl(context)+"/Usercenter/ygroupadd";
		String id = addGroup(context,path,groupName);
		try {
			int parseInt = Integer.parseInt(id.trim());
			cloudgroupMap.put(groupName, parseInt);
		} catch (Exception e) {
			throw new RuntimeException("增加分组失败");
		}
		Log.i("test", "cloudgroupMap="+cloudgroupMap.toString());
	}
	/**
	 * 增加通讯录分组
	 * @param userId
	 * @param groupName
	 * @throws Exception
	 */
	public static void addContactGroup(Context context,String groupName) throws Exception {
		String g = URLEncoder.encode(groupName, "UTF-8");
		String path=LoginUtil.getBaseUrl(context)+"/Usercenter/groupadd";
		String id = addGroup(context,path,groupName);
		try {
			int parseInt = Integer.parseInt(id.trim());
			contactgroupMap.put(groupName, parseInt);
		} catch (Exception e) {
			throw new RuntimeException("增加分组失败");
		}
		Log.i("test", "contactgroupMap="+contactgroupMap.toString());
	}
	/**
	 * 增加分组
	 * @param path
	 * @return
	 * @throws Exception
	 */
	private static String addGroup(Context context,String path,String groupName) throws Exception {
		
		DefaultHttpClient client=new DefaultHttpClient();
		int userId=LoginUtil.getLastLoginUserId(context);
		String token=LoginUtil.getLastLoginToken(context);
		String verify=MD5Util.phpMD5Encode(userId+""+groupName+LoginUtil.KEY);
		
		HttpPost post=new HttpPost(path);
		
		ArrayList<NameValuePair> valuePairs=new ArrayList<NameValuePair>();
		BasicNameValuePair valuePair=new BasicNameValuePair("appcode",LoginUtil.APPCODE);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("verify",verify);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("datatype","j");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("token",token);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("userid",userId+"");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("groupname",groupName);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("isreadonly",0+"");
		valuePairs.add(valuePair);
		
		UrlEncodedFormEntity entity=new UrlEncodedFormEntity(valuePairs, "UTF-8");
		post.setEntity(entity);
		HttpResponse response = client.execute(post);
		HttpEntity httpEntity = response.getEntity();
		if (httpEntity!=null) {
			byte[] data = JsonUtil.getDataFromInputStream(httpEntity.getContent());
			if (data!=null&&data.length>0) {
				String json=new String(data, "UTF-8");
				Log.i("test", "json="+json);
				if (json.trim().startsWith("{\"code\"")) {
					JSONObject obj=new JSONObject(json);
					switch (obj.optInt("code")) {
					case 1:
						break;
					default:
						throw new RuntimeException(obj.optString("description"));
					}
				}
				return json;
			}
		}
		return null;
	}
	/**
	 * 编辑分组，云好友分组、通讯录好友分组都掉这个方法
	 * @param oldName
	 * @param newName
	 * @param type 使用本类的常量
	 * @throws Exception
	 */
	public static void  editGroup(Context context,String oldName,String newName,int type) throws Exception {

		int groupId=getGroupIdByName(type,oldName);
		String path=LoginUtil.getBaseUrl(context)+"/Usercenter/groupedit";
		int userId=LoginUtil.getLastLoginUserId(context);
		String token=LoginUtil.getLastLoginToken(context);
		String verify=MD5Util.phpMD5Encode(userId+""+newName+LoginUtil.KEY);
		
		DefaultHttpClient client=new DefaultHttpClient();
		HttpPost post=new HttpPost(path);
		
		ArrayList<NameValuePair> valuePairs=new ArrayList<NameValuePair>();
		BasicNameValuePair valuePair=new BasicNameValuePair("appcode",LoginUtil.APPCODE);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("verify",verify);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("datatype","j");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("token",token);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("userid",userId+"");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("groupid",groupId+"");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("groupname",newName);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("isreadonly","0");
		valuePairs.add(valuePair);
		
		UrlEncodedFormEntity entity=new UrlEncodedFormEntity(valuePairs, "UTF-8");
		post.setEntity(entity);
		HttpResponse response = client.execute(post);
		HttpEntity httpEntity = response.getEntity();
		if (httpEntity!=null&&JsonUtil.parseBasePostResponse(context, httpEntity.getContent())) {
			HashMap<String,Integer> map=type==CLOUD?cloudgroupMap:contactgroupMap;
			map.remove(oldName);
			map.put(newName, groupId);
		}else {
			throw new RuntimeException("删除分组失败");
		}
	}
	/**
	 * 
	 * @param type 1是云好友，0是通讯录好友，可以使用该类的两个常量
	 * @param groupName
	 * @return
	 */
	public static int getGroupIdByName(int type,String groupName) {
		HashMap<String, Integer> map=null;
		if (type==CLOUD) {
			map=cloudgroupMap;
		}else if(type==CONTACTS){
			map=contactgroupMap;
		}
		Integer id = map.get(groupName);
		Log.i("test","getGroupIdByName+++++++++"+map.toString());
		Log.i("test","getGroupIdByName+++++++++"+groupName);
		Log.i("test","getGroupIdByName+++++++++"+id);
		if (id!= null) {
			return id;
		}
		return -1;
	}
	/**
	 * 删除分组
	 * @param groupName
	 * @param type 使用本类的常量来区别
	 * @throws Exception
	 */
	public static void  deleteGroup(Context context,String groupName,int type) throws Exception {
		int groupId=getGroupIdByName(type, groupName);
		String path=LoginUtil.getBaseUrl(context)+"/Usercenter/groupdel";
		
		DefaultHttpClient client=new DefaultHttpClient();
		int userId=LoginUtil.getLastLoginUserId(context);
		String token=LoginUtil.getLastLoginToken(context);
		String verify=MD5Util.phpMD5Encode(userId+""+groupId+LoginUtil.KEY);
		
		HttpPost post=new HttpPost(path);
		
		ArrayList<NameValuePair> valuePairs=new ArrayList<NameValuePair>();
		BasicNameValuePair valuePair=new BasicNameValuePair("appcode",LoginUtil.APPCODE);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("verify",verify);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("datatype","j");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("token",token);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("userid",userId+"");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("groupid",groupId+"");
		valuePairs.add(valuePair);
		
		UrlEncodedFormEntity entity=new UrlEncodedFormEntity(valuePairs, "UTF-8");
		post.setEntity(entity);
		HttpResponse response = client.execute(post);
		HttpEntity httpEntity = response.getEntity();
		if (httpEntity!=null&&JsonUtil.parseBasePostResponse(context, httpEntity.getContent())) {
			HashMap<String,Integer> map=type==CLOUD?cloudgroupMap:contactgroupMap;
			map.remove(groupName);
		}else {
			throw new RuntimeException("删除分组失败");
		}
	}
	
	/**
	 * 导入系统通讯录联系人
	 * @param ctx
	 * @return
	 * @throws Exception
	 */
	public static ArrayList<Contact> importSystemContacts(Context ctx) throws Exception {
		Cursor cur = ContactsUtils.getAllContacts(ctx);
		ArrayList<Contact> contacts=new ArrayList<Contact>();
		JSONArray array=new JSONArray();
		if (cur.moveToFirst()) {
			do {
				String _id = cur.getString(0);
				Contact contact = ContactsUtils.populateContact(ctx, _id);
				JSONObject jsonObject = JsonUtil.translateContactToJson(contact);
				array.put(jsonObject);
			} while (cur.moveToNext());
			cur.close();
			String file = FileUtil.saveJsontoFile(ctx, LoginUtil.getLastLoginUserId(ctx)+"", array.toString());
			Log.i("test", file);
			String path=LoginUtil.getBaseUrl(ctx)+"/Usercenter/zip";
			File ff=new File(file);
			HashMap<String, String> params=new HashMap<String, String>();
			FormFile formFile=new FormFile(ff.getName(), ff, "file", "application/octet-stream");
			SocketHttpRequester.post(path, params, formFile);
			ArrayList<Contact> list = getContactFriends(ctx, LoginUtil.getLastLoginUserId(ctx));
			if (list!=null) {
				contacts.addAll(list);
			}
			return contacts;
		}
		throw new RuntimeException("导入联系人失败");
	}
	
	
	/**
	 * 下载新的apk并安装
	 * @param context
	 * @param version
	 * @throws IOException 
	 * @throws Exception
	 */
	public static void downloadNewSoftware(Context context,float version) throws Exception{
		
		String path=LoginUtil.getBaseUrl(context)+"/Usercenter/download?version="+version;
		URL url=new URL(path);
		HttpURLConnection conn=(HttpURLConnection) url.openConnection();
		path = new String(JsonUtil.getDataFromInputStream(conn.getInputStream()));
		Log.i("test", "path="+path);
		conn.disconnect();
		if (path.trim().equals("已是最新版本")) {
			throw new RuntimeException("已是最新版本");
		}
		if (path.startsWith("http://")) {
			url=new URL(path);
			String dir = FileUtil.createDir(context);
			File file=new File(dir, url.getFile());
			file.getParentFile().mkdirs();
			file.createNewFile();
			conn=(HttpURLConnection) url.openConnection();
			FileOutputStream fos=new FileOutputStream(file);
			InputStream ins = conn.getInputStream();
			int len=0;
			byte[] buffer=new byte[1024];
			
			while((len=ins.read(buffer))!=-1)
			{
				fos.write(buffer, 0, len);
				fos.flush();
			}
			ins.close();
			fos.close();
			Intent intent = new Intent();    
			// 设置目标应用安装包路径    
			intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent);
		}
	}
	/**
	 * type为0，查找通讯录好友，为1，查找云好友
	 * @param type
	 * @param keyword
	 * @return
	 */
	public static ArrayList<Contact> searchContacts(int type,String keyword){
		final ArrayList<Contact> contacts=type==CONTACTS?MainActivity.contactFriends:MainActivity.cloudFriends;
		ArrayList<Contact> results=new ArrayList<Contact>();
		int size = contacts.size();
		for(int i=0;i<size;i++){
			Contact contact = contacts.get(i);
			String displayName=contact.displayName;
			if (displayName.indexOf(keyword)!=-1){
				results.add(contact);				
				continue;		
			}else{
				String value=keyword.toLowerCase();
				if(PinYinUtil.hanzi2PinYin(displayName).indexOf(value)!=-1||
						PinYinUtil.hanzi2PinYinOnlyHead(displayName).indexOf(value)!=-1){
					results.add(contact);				
					continue;	
				}
			}
			if (contact.fphone!=null&&contact.fphone.indexOf(keyword)!=-1) {
				results.add(contact);
				continue;
			}
			final ArrayList<ContactData> phones=contact.phones;
			int s=phones.size();
			for(int j=0;j<s;j++){
				if (phones.get(j).content.indexOf(keyword)!=-1) {
					results.add(contact);
					continue;
				}
			}
			if (contact.femail!=null&&contact.femail.indexOf(keyword)!=-1) {
				results.add(contact);
				continue;
			}
			final ArrayList<ContactData> emails=contact.emails;
			s=emails.size();
			for(int j=0;j<s;j++){
				if (emails.get(j).content.indexOf(keyword)!=-1) {
					results.add(contact);
					continue;
				}
			}
		}
		return results;
	}
	
	public static String uploadPhoto(Context context,File file) throws Exception{
		String path=LoginUtil.getBaseUrl(context)+"/Usercenter/photofile";
		HashMap<String, String> params=new HashMap<String, String>();
		FormFile formFile=new FormFile(file.getName(), file, "file", "image/*");
		String url = SocketHttpRequester.post(path, params, formFile);
		if (url==null) {
			throw new Exception("头像上传失败");
		}
		return url;
	}
	
	public static boolean netIsValidate(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo network = cm.getActiveNetworkInfo();
		if (network != null && network.isAvailable()) {
			Log.i("network", true+" ");
			return true;
		}else
		{
			return false;
		}
		
	}
}
